<?php
class Gallery_Model extends General_Model {
	protected $_gallery_path = "";

	function __construct() {
		parent::__construct();
		$this->_gallery_path = realpath(APPPATH. "./storage/images");
	}
	public function do_upload() {
		$config = array ('upload_path' => $this->_gallery_path,
							'allowed_types' => 'gif|jpg|png|jpeg',
							'max_size' => '5000' );
		$this->load->library ( "upload", $config );
		if (! $this->upload->do_upload ( "img" )) {
			$image_data = array ($this->upload->display_errors () );
		} else {
			$image_data = $this->upload->data ();
		}
		return $image_data;
	}

	function getList($type) {
		$this->db->where ( 'type', $type );
		$query = $this->db->get ( 'gallery' );
		$list = $query->result ();
		return $list;
	}

	function getListAlbum(){
		$query = $this->db->get ( 'album' );
		$list = $query->result ();
		return $list;
	}
	function getListPhotos($albumid) {
		$this->db->where ( 'albumid', $albumid );
		$query = $this->db->get ( 'photos' );
		$detail = $query->result ();
		return $detail;
	}

	function delphoto($id) {
		$this->db->where ( 'id', $id );
		$this->db->limit ( 1, 0 );
		$this->db->delete ( 'photos' );
		return TRUE;
	}
	function del($id) {
		$this->db->where ( 'id', $id );
		$this->db->limit ( 1, 0 );
		$this->db->delete ( 'gallery' );
		return TRUE;
	}
	function delalbum($id) {
		$this->db->where ( 'id', $id );
		$this->db->limit ( 1, 0 );
		$this->db->delete ( 'album' );
		return TRUE;
	}

	function save_video_db($data) {
	    $ten = $data['ten_dat'];
		$ten_file = $data['ten_file_video'];
		$sql_video="insert into ci_video(id,ten_dat,ten_file_video) values('','".$ten."','".$ten_file."')";
		$this->db->query($sql_video);
	}

	function getlistvideo(){
	  $sql_get = "select * from ci_video";
	  $query = $this->db->query($sql_get);
	  $data_video=$query->result_array();
	  return $data_video;
	}

	function getRandomPhoto($limit=6,$albumid=''){
		$filter='';
		if($albumid!=''){
			$filter="WHERE albumid='$albumid'";
		}
		$query=$this->db->query("SELECT * FROM `ci_photos` $filter ORDER BY RAND() LIMIT $limit");
		$photos = $query->result ();
		return $photos;
	}
	function getVideoDetail($id){
		$this->db->where ( 'id', $id );
		$query = $this->db->get ( 'video' );
		$data = $query->row();
		return $data;
	}
	
	function getHompageVideo(){
		$this->db->where ( 'homepage', '1' );
		$query = $this->db->get ( 'video' );
		$list = $query->result_array ();
		return $list;
	}
	

}